#!/usr/bin/perl 
use CGI qw(:standard);
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
use strict;
use FileHandle;
use CGI::Carp 'fatalsToBrowser';
use Cwd;
use File::Slurp;

my $email = "yinche\@cisco.com";
my $url = "http://172.18.138.102/cgi-bin/wMOS.cgi";
my $client_ip = $ENV{'REMOTE_ADDR'};
my $host = $ENV{'SERVER_NAME'};
my @session_log;
my $clients_info;
my $info_text;
my $cur_dir = getcwd(); 
my $perl = '/usr/bin/perl';
my $script_name = $0;
$script_name =~ s/$cur_dir//;
my $d = "[0-9A-Fa-f]";
my $dd = "$d$d";
print header;
print start_html("wMOS");
my @fields = param;
if (@fields ==0) {
	my $timestamp = time();
#	@session_log = `$perl $cur_dir/getClients.pl $timestamp`;
#	print @session_log;
	print <<EndHTML;
	<h2>wMOS Test</h2>

	<p> Local IP address: $client_ip </p>
	<p> Session Log: </p>
	$info_text 
	<p> Please Click the 'start MOS' button to start the test </p>
EndHTML
	print start_form(-method=>'POST', -action=>"/cgi-bin/$script_name"),"<em> Mac Address (optional) : </em>", textfield('client_mac', 'FF:FF:FF:FF:FF:FF', 17, 17), hidden('timestamp', $timestamp),"<br>", submit(-name=>'sub_form', -value=>'Start MOS'), end_form, hr;
} elsif (param('wMOS')) {
	
	my $wMOS = param('wMOS');
	my $client_mac = param('client_mac');
	
	my $url  = "http://$host/wMOS";
	

	print <<ENDHTML;
	<p>The wMOS test has finished, test Session details is listed below </p> 
	<p>wMOS Score: $wMOS </p>
	<p>Links to the session logs:<p>
ENDHTML
} else {
	my $timestamp = param('timestamp');
	my $client_mac = param('client_mac');
	print "<p> Collecting Client Info please Waiting page to be loaded </p> ";
	if( $client_mac =~ /($dd){6}|$dd(([:-])$dd){5}/){
		print "<p> $client_mac </p>";
	}
#	system("$perl $cur_dir/collectData.pl");	
	@session_log = `$perl $cur_dir/getClients.pl $timestamp`;
	print <<ENDHTML;

	<p> wMOS Testing </p>
	<body onload="init()" style="height:100%; display:block">
      <div id="videoWindow"> 
	<video poster="http://content.bitsontherun.com/thumbs/nfSyO85Q-320.jpg" height="270" width="480" controls>
    <source src="http://devimages.apple.com/iphone/samples/bipbop/gear1/prog_index.m3u8" type="application/vnd.apple.mpegurl" />
    <source src="http://content.bitsontherun.com/videos/nfSyO85Q-27m5HpIu.webm" type="video/webm" />
    <source src="http://content.bitsontherun.com/videos/nfSyO85Q-52qL9xLP.mp4" type="video/mp4" />
    <p class="warning">Your browser does not support HTML5 video.</p>
</video>
</p>
      </div>
	<div id="windDown">
	The testing is underway.<br/>
	Please play and watch the video in the window, above.<br/>
	When the video ends, please enter the score for your experience in watching the video into the box below.<br/>
	Then, press the button to end the test.<br/>
	<table border="1">
	  <tr>
	    <td colspan="3">Scoring Chart</td>
	  </tr>
	  <tr>
	    <td>Score</td><td>Quality</td><td>Impairment</td>
	  </tr>
	  <tr>
	    <td>5</td><td>Excellent</td><td>Imperceptible</td>
	  </tr>
	  <tr>
	    <td>4</td><td>Good</td><td>Perceptible but not annoying</td>
	  </tr>
	  <tr>
	    <td>3</td><td>Fair</td><td>Slightly annoying</td>
	  </tr>
	  <tr>
	    <td>2</td><td>Poor</td><td>Annoying</td>
	  </tr>
	  <tr>
	    <td>1</td><td>Bad</td><td>Very annoying</td>
	  </tr>
	</table>
      </div>
ENDHTML
      print start_form(-method=>'POST', -action=>"/cgi-bin/$script_name"), "Wi-Fi Quality Score: <input type='text' name='wMOS' style='background-color:#addfff' value='' size='4'/>",  hidden('timestamp', $timestamp), submit(-name=>'sub_form', -value=>'Submit'), end_form, hr;
#	<input type="button" value="End Testing" onclick="endTesting()"/><br/>
	
	exec("$perl $cur_dir/collectData.pl $timestamp");
}
print end_html;
